//
//  PPAlbumPictureInsertionTableViewAlbumsCell.m
//  pickpic
//
//  Created by Jinxiao on 10/2/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumPictureInsertionTableViewAlbumsCell.h"

#import "PPAlbumPictureInsertionAlbumView.h"

#define kEdgeInsets UIEdgeInsetsMake(13, 14, 13, 14)
#define kSpacing 3

@interface PPAlbumPictureInsertionTableViewAlbumsCell ()
@property (readwrite, nonatomic, strong) UIButton *addButton;
@property (readwrite, nonatomic, strong) NSArray *albumViews;
@end

@implementation PPAlbumPictureInsertionTableViewAlbumsCell

- (void)awakeFromNib {
    [super awakeFromNib];

    self.selectionStyle = UITableViewCellSelectionStyleNone;

    _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _addButton.backgroundColor = [UIColor orangeColor];
    [_addButton setBackgroundImage:[[UIImage imageWithColor:[UIColor colorWithRGBHex:0xffdd17]] stretchableImageByCenter] forState:UIControlStateNormal];
    [_addButton setImage:[UIImage imageNamed:@"icon-add-white"] forState:UIControlStateNormal];
    [self addSubview:_addButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    [_albumViews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    NSMutableArray *albumViews = [NSMutableArray array];

    for(NSInteger index = 0; index < _albums.count; index++) {
        PPAlbumModel *album = _albums[index];

        PPAlbumPictureInsertionAlbumView *albumView = [[PPAlbumPictureInsertionAlbumView alloc] init];
        albumView.album = album;
        [self addSubview:albumView];

        [albumViews addObject:albumView];

        __weak typeof(self)weakSelf = self;
        [albumView.selectButton setActionBlock:^(UIControl *control) {
            control.selected = !control.selected;

            [weakSelf.delegate albumsCell:weakSelf didChangeSelectionState:control.selected atIndex:index];
        } forControlEvents:UIControlEventTouchUpInside];
    }

    _albumViews = albumViews;

    for(NSInteger index = 0; index < _albumViews.count; index++) {
        PPAlbumPictureInsertionAlbumView *albumView = _albumViews[index];
        albumView.frame = [[self class] tileFrameForIndex:index boundingSize:CGSizeMake(self.width, CGFLOAT_MAX)];
    }

    CGRect addFrame = [[self class] tileFrameForIndex:_albumViews.count boundingSize:CGSizeMake(self.width, CGFLOAT_MAX)];
    _addButton.frame = CGRectMake(addFrame.origin.x, addFrame.origin.y + 6, addFrame.size.width - 6, addFrame.size.height - 6);
}

- (void)setAlbums:(NSArray<PPAlbumModel *> *)albums {
    _albums = albums;

    [self setNeedsLayout];
}

+ (CGFloat)heightForNumberOfAlbums:(NSInteger)numberOfAlbums boundingSize:(CGSize)boundingSize {

    numberOfAlbums++;

    if(numberOfAlbums == 0) {
        return kEdgeInsets.top + kEdgeInsets.bottom;
    } else {
        CGSize tileSize = [self tileSizeForBoundingSize:boundingSize];

        NSInteger rows = ceil(numberOfAlbums/4.0);

        if(rows == 1) {
            return kEdgeInsets.top + kEdgeInsets.bottom + tileSize.height;
        } else {
            return kEdgeInsets.top + kEdgeInsets.bottom + tileSize.height*rows + kSpacing*(rows - 1);
        }
    }
}

+ (CGRect)tileFrameForIndex:(NSInteger)index boundingSize:(CGSize)boundingSize {
    CGSize tileSize = [self tileSizeForBoundingSize:boundingSize];

    NSInteger row = floor(index/4.0);
    NSInteger column = index%4;

    return CGRectMake(kEdgeInsets.left + column*(tileSize.width + kSpacing), kEdgeInsets.top + row*(tileSize.height + kSpacing), tileSize.width, tileSize.height);
}

+ (CGSize)tileSizeForBoundingSize:(CGSize)boundingSize {
    CGFloat width = (boundingSize.width - kEdgeInsets.left - kEdgeInsets.right - kSpacing*3)/4;
    CGFloat height = width;

    return CGSizeMake(width, height);
}

@end
